from lib.gongmall.sign import SignUtil
from lib.gongmall.http import HttpHelper
import traceback
import logging
app_logger = logging.getLogger("student")
import time, json, datetime


def doWithdraw(mobile, name, amount, identity, bankAccount, dateTime):
    """
    提现
    :param mobile:
    :param name:
    :param amount:
    :param identity:
    :param bankAccount:
    :return:
    """
    try:
        url = "/api/withdraw/doWithdraw"
        post_data = {
            "requestId": int(time.time()),
            "mobile": mobile,
            "name": name,
            "amount": round(amount/100, 2),
            "identity": identity,
            "bankAccount": bankAccount,
            "remark": "零钱提现",
            "dateTime":  datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        }
        common = SignUtil.getCommonData()
        post_data.update(common)
        sign = SignUtil.getSign(parms=post_data)
        post_data['sign'] = sign
        result = HttpHelper.doPOST(url, post_data)
        withdraw_data = json.loads(result)
        app_logger.error("这是工猫提现返回----")
        app_logger.error(withdraw_data)
        return withdraw_data
    except:
        err = traceback.format_exc()
        app_logger.error(err)
        return err


def getContractStatus(name, identity):
    """
    查询电签结果
    :param name:
    :param identity:
    :return:
    """
    try:
        url = "/api/employee/getContractStatusV2"
        post_data = {
            "name": name,
            "identity": identity,
        }
        common = SignUtil.getCommonData()
        post_data.update(common)
        sign = SignUtil.getSign(parms=post_data)
        post_data['sign'] = sign
        result = HttpHelper.doPOST(url, post_data)
        contract_data = json.loads(result)
        return contract_data
    except:
        err = traceback.format_exc()
        app_logger.error(err)
        return err


def syncBankAccount(name, mobile, identity, salaryType, bankAccount):
    """
    修改员工银行卡
    :param name:
    :param mobile:
    :param identity:
    :param salaryType:
    :param bankAccount:
    :return:
    """
    try:
        url = "/api/employee/v2/syncBankAccount"
        post_data = {
            "name": name,
            "identity": identity,
            "mobile": mobile,
            "salaryType": salaryType,
            "bankAccount": bankAccount
        }
        common = SignUtil.getCommonData()
        post_data.update(common)
        sign = SignUtil.getSign(parms=post_data)
        post_data['sign'] = sign
        result = HttpHelper.doPOST(url, post_data)
        contract_data = json.loads(result)
        return contract_data
    except:
        err = traceback.format_exc()
        app_logger.error(err)
        return err


def getUrlContract(name, mobile, certificateType, idNumber, bankNum, extraParam=None):
    """
    获取电签链接
    :param name:
    :param mobile:
    :param certificateType:
    :param idNumber:
    :param bankNum:
    :param extraParam:
    :return:
    """
    try:
        post_data = {
            "name": name,
            "mobile": mobile,
            "certificateType": certificateType,
            "idNumber": idNumber,
            "bankNum": bankNum,
            "extraParam": extraParam
        }
        url = SignUtil.contract(post_data)
        return url
    except:
        err = traceback.format_exc()
        app_logger.error(err)
        return err